﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLT_BLL;
using QLT_Public;

namespace GiaoDien
{
    public partial class frm_NhanVien1 : DevExpress.XtraEditors.XtraForm
    {
        public frm_NhanVien1()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaNV;

        public int MaNV
        {
            get { return _MaNV; }
            set { _MaNV = value; }
        }
        private string _HoTen;

        public string HoTen
        {
            get { return _HoTen; }
            set { _HoTen = value; }
        }
        private DateTime _NgaySinh;

        public DateTime NgaySinh
        {
            get { return _NgaySinh; }
            set { _NgaySinh = value; }
        }
        private string _GioiTinh;

        public string GioiTinh
        {
            get { return _GioiTinh; }
            set { _GioiTinh = value; }
        }
        private int _CMT;

        public int CMT
        {
            get { return _CMT; }
            set { _CMT = value; }
        }
        private string _DiaChi;

        public string DiaChi
        {
            get { return _DiaChi; }
            set { _DiaChi = value; }
        }
        private string _DienThoai;

        public string DienThoai
        {
            get { return _DienThoai; }
            set { _DienThoai = value; }
        }

        private string _Email;

        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }
        private string _MaNQ;

        public string MaNQ
        {
            get { return _MaNQ; }
            set { _MaNQ = value; }
        }
        
        private string _TenDangNhap;

        public string TenDangNhap
        {
            get { return _TenDangNhap; }
            set { _TenDangNhap = value; }
        }
        private string _MatKhau;

        public string MatKhau
        {
            get { return _MatKhau; }
            set { _MatKhau = value; }
        }
        public void GetData()
        {
            txtMaNV.Text = MaNV.ToString();
            txtHoTen.Text = HoTen;
            dateTimeNgaySinh.Text = NgaySinh.ToString();
            cmbGioiTinh.Text = GioiTinh;
            maskedCMT.Text = CMT.ToString();
            txtDiaChi.Text = DiaChi;
            maskedDienThoai.Text = DienThoai.ToString();
            txtEmail.Text = Email;
            cmbTenNQ.Text = MaNQ.ToString();
            txtTenDangNhap.Text = TenDangNhap;
            txtMatKhau.Text = MatKhau;
            string sql = "select*from tbl_NhomQuyen, tbl_NhanVien where tbl_NhanVien.MaNQ=tbl_NhomQuyen.MaNQ and MaNV='" + txtMaNV.Text + "'";
            DataTable dt = new DataTable();
            dt = nhanVien_bll.taoBang(sql);
            cmbMaNQ.DataSource = dt;
            cmbMaNQ.DisplayMember = "MaNQ";
            cmbTenNQ.DataSource = dt;
            cmbTenNQ.DisplayMember = "TenNQ";
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        public void EnabledText(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled = false;
            }
            foreach (Control i in ctr.Controls)
            {
                EnabledText(i);
            }
        }
        public void EnabledText_True(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled = true;
            }
            foreach (Control i in ctr.Controls)
            {
                EnabledText_True(i);
            }
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
            {
                Huy_Text(i);
            }
        }
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Nhân Viên";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Thông Tin Nhân Viên";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem Thông Tin Nhân Viên";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                bbi_Luu.Enabled = false;
                EnabledText(this);
                dateTimeNgaySinh.Enabled = false;
                cmbGioiTinh.Enabled = false;
                maskedCMT.Enabled = false;
                maskedDienThoai.Enabled = false;
                cmbTenNQ.Enabled = false;
                GetData();
            }
        }

        private void frm_NhanVien_Load(object sender, EventArgs e)
        {
            LoadForm();
            cmbMaNQ.Hide();
        }
        NhatKy_Bll nhatKy_bll = new NhatKy_Bll();
        NhanVien_BLL nhanVien_bll = new NhanVien_BLL();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NhatKy_Public nk = new NhatKy_Public();
            NhanVien_Public t = new NhanVien_Public();
            if (txtHoTen.Text != "" && cmbGioiTinh.Text != "" && maskedCMT.Text != "" && txtDiaChi.Text != "" && cmbTenNQ.Text != "")
            {
                if (Flag == 0)
                {
                    try
                    {
                        //them
                        t.HoTen = txtHoTen.Text;
                        t.NgaySinh = Convert.ToDateTime(dateTimeNgaySinh.Text);
                        t.GioiTinh = cmbGioiTinh.Text;
                        t.CMT = Convert.ToInt32(maskedCMT.Text);
                        t.DiaChi = txtDiaChi.Text;
                        t.DienThoai = maskedDienThoai.Text;
                        t.Email = txtEmail.Text;
                        t.MaNQ = int.Parse(cmbMaNQ.Text);
                        t.TenDangNhap = txtTenDangNhap.Text;
                        t.MatKhau = txtMatKhau.Text;
                        nhanVien_bll.insert_NhanVien(t, t, t, t, t, t, t, t, t, t);
                        string d = DateTime.Now.ToString();
                        Class_TrungGian tg = new Class_TrungGian();
                        nk.TaiKhoan = tg.TaiKhoan;
                        nk.NgayGio = d;
                        nk.DoiTuong = tg.DoiTuong;
                        nk.HanhDong = "Thêm Mới";
                        nk.MoTa = "Thêm mới Nhân viên: '" + txtHoTen.Text + "'";
                        nhatKy_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                        XtraMessageBox.Show("Đã Thêm Nhân Viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        maskedCMT.ResetText();
                        maskedDienThoai.ResetText();
                        cmbGioiTinh.ResetText();
                        cmbTenNQ.ResetText();
                        Huy_Text(this);
                    }

                    catch (Exception)
                    {
                        XtraMessageBox.Show("Thực hiện không thành công!Bạn hãy nhập lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (Flag == 1)
                {
                    try
                    {
                        //sua
                        t.MaNV = int.Parse(txtMaNV.Text);
                        t.HoTen = txtHoTen.Text;
                        t.NgaySinh = Convert.ToDateTime(dateTimeNgaySinh.Text);
                        t.GioiTinh = cmbGioiTinh.Text;
                        t.CMT = int.Parse(maskedCMT.Text);
                        t.DiaChi = txtDiaChi.Text;
                        t.DienThoai = maskedDienThoai.Text;
                        t.Email = txtEmail.Text;
                        t.MaNQ = int.Parse(cmbMaNQ.Text);
                        t.TenDangNhap = txtTenDangNhap.Text;
                        t.MatKhau = txtMatKhau.Text;
                        nhanVien_bll.update_NhanVien(t, t, t, t, t, t, t, t, t, t, t);
                        string d = DateTime.Now.ToString();
                        Class_TrungGian tg = new Class_TrungGian();
                        nk.TaiKhoan = tg.TaiKhoan;
                        nk.NgayGio = d;
                        nk.DoiTuong = tg.DoiTuong;
                        nk.HanhDong = "Sửa";
                        nk.MoTa = "Sửa thông tin Mã Nhân viên: '" +txtMaNV.Text + "' có tên: '"+txtHoTen.Text+"'";
                        nhatKy_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                        XtraMessageBox.Show("Đã Sửa Nhân Viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        maskedCMT.ResetText();
                        maskedDienThoai.ResetText();
                        cmbGioiTinh.ResetText();
                        cmbTenNQ.ResetText();
                        Huy_Text(this);
                    }

                    catch (Exception)
                    {
                        XtraMessageBox.Show("Thực hiện không thành công!Bạn hãy nhập lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (Flag == 2)
                {
                    try
                    {
                        //sua khi xem
                        t.MaNV = int.Parse(txtMaNV.Text);
                        t.HoTen = txtHoTen.Text;
                        t.NgaySinh = Convert.ToDateTime(dateTimeNgaySinh.Text);
                        t.GioiTinh = cmbGioiTinh.Text;
                        t.CMT = int.Parse(maskedCMT.Text);
                        t.DiaChi = txtDiaChi.Text;
                        t.DienThoai = maskedDienThoai.Text;
                        t.Email = txtEmail.Text;
                        t.MaNQ = int.Parse(cmbMaNQ.Text);
                        t.TenDangNhap = txtTenDangNhap.Text;
                        t.MatKhau = txtMatKhau.Text;
                        nhanVien_bll.update_NhanVien(t, t, t, t, t, t, t, t, t, t, t);
                        XtraMessageBox.Show("Đã Sửa Nhân Viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        maskedCMT.ResetText();
                        maskedDienThoai.ResetText();
                        cmbGioiTinh.ResetText();
                        cmbTenNQ.ResetText();
                        Huy_Text(this);
                    }

                    catch (Exception)
                    {
                        XtraMessageBox.Show("Thực hiện không thành công!Bạn hãy nhập lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            MyGetData(1);

        }

        private void cmbMaNQ_Click(object sender, EventArgs e)
        {
            string sql = "select * from tbl_NhomQuyen";
            DataTable dt = new DataTable();
            dt = nhanVien_bll.taoBang(sql);
            cmbMaNQ.DataSource = dt;
            cmbMaNQ.DisplayMember = "MaNQ";
            cmbTenNQ.DataSource = dt;
            cmbTenNQ.DisplayMember = "TenNQ";
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            maskedCMT.ResetText();
            maskedDienThoai.ResetText();
            cmbGioiTinh.ResetText();
            cmbTenNQ.ResetText();
            dateTimeNgaySinh.Enabled = true;
            cmbGioiTinh.Enabled = true;
            maskedCMT.Enabled = true;
            maskedDienThoai.Enabled = true;
            cmbTenNQ.Enabled = true;
            EnabledText_True(this);
            Flag = 11;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            txtMaNV.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Them.Enabled = false;
            bbi_Huy.Enabled = true;
            bbi_Luu.Enabled = true;
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            txtMaNV.Enabled = true;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }

        //private void txtMaNV_Leave(object sender, EventArgs e)
        //{
        //    if (txtMaNV.Text == "")
        //        txtMaNV.Focus();
        //}

        private void txtHoTen_Leave(object sender, EventArgs e)
        {
            if (txtHoTen.Text == "")
                txtHoTen.Focus();
        }

        private void maskedCMT_Leave(object sender, EventArgs e)
        {
            if (maskedCMT.Text == "")
                maskedCMT.Focus();
        }

        private void txtDiaChi_Leave(object sender, EventArgs e)
        {
            if (txtDiaChi.Text == "")
                txtDiaChi.Focus();
        }

        private void maskedDienThoai_Leave(object sender, EventArgs e)
        {
            if (maskedDienThoai.Text == "")
                maskedDienThoai.Focus();
        }

        private void txtEmail_Leave(object sender, EventArgs e)
        {
            if (txtEmail.Text == "")
                txtEmail.Focus();
        }

        private void cmbTenNQ_Leave(object sender, EventArgs e)
        {
            if (cmbTenNQ.Text == "")
                cmbTenNQ.Focus();
        }

        private void txtTenDangNhap_Leave(object sender, EventArgs e)
        {
            if (txtTenDangNhap.Text == "")
                txtTenDangNhap.Focus();
        }

        private void txtMatKhau_Leave(object sender, EventArgs e)
        {
            if (txtMatKhau.Text == "")
                txtMatKhau.Focus();
        }

        private void maskedCMT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void maskedDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

    }
}